#!/usr/bin/env python

import pypvm
import sys
import os
from signal import SIGTERM
from sys import stdout
from time import sleep

try:

# ============================ test mytid () ==================================
    my_tid = pypvm.mytid ()
    print "MASTER: my tid = %s" % my_tid

# ============================ test get/setopt () =============================
    print "MASTER: testing get/setopt ()"
    val = pypvm.setopt (pypvm.opt['Route'], pypvm.getopt (pypvm.opt['Route']))
    print "PvmRoute =", val

# ============================ test catchout () ===============================
    print "MASTER: setting catchout to stdout..."
    pypvm.catchout (stdout)

# ============================ test delhosts () ===============================
# print "MASTER: removing host hopper..."
# pypvm.delhosts (["hopper"])

# ============================ test addhosts () ===============================
# print "MASTER: adding host hopper..."
# pypvm.addhosts (["hopper"])

# ============================ test spawn () ==================================
    print "MASTER: spawning 2 tasks (prueba_esclavo03.py)..."
    the_file = os.getcwd() + "/prueba_esclavo03.py"
    print the_file
    tids = pypvm.spawn (the_file, [], pypvm.spawnOpts['TaskDefault'], "", 2)
    print "MASTER: spawned task's tid = %s" % tids

# ============================ test notify () =================================
    print "MASTER: asking for notification on child exits..."
    pypvm.notify (pypvm.notifyDict['TaskExit'], 2, tids, 1)

# ============================ test initsend () ===============================
    print "MASTER: initializing message buffer to PvmDataDefault..."
    pypvm.initsend (pypvm.data['default'])

# ============================ test pk () =====================================
    data = {-1: [1, (2, 3, 4), 4]}
    print ("MASTER: packing %s..." % data)
    pypvm.pk (data)

# ============================ test send () ===================================
    for recipient in tids:
        print ("MASTER: sending data to tid %s..." % recipient)
        pypvm.send (recipient, 1)

# ============================ test config () =================================
    print "MASTER: results from config:", pypvm.config (1)

# ============================ test tasks () ==================================
    print "MASTER: results from tasks:", pypvm.tasks (0)

# ============================ test tidtohost () ==============================
    print "MASTER: results from tidtohost:", pypvm.tidtohost (my_tid)

# ============================ sleep ==========================================
    print "MASTER: sleeping for 2 seconds (so slaves can catch up)..."
    sleep (2)

# ============================ test sendsig () ================================
    print "MASTER: sending SIGTERM to children..."
    for tid in tids:
        pypvm.sendsig (tid, SIGTERM)

# ============================ test nrecv () ==================================
#    print "MASTER: calling nrecv ()..."
#    pypvm.nrecv (tids[0], 1)

# ============================ test trecv () ==================================
# NOTE: this also serves to sleep for 10 seconds, since no message is coming.
#    print "MASTER: calling trecv ()..."
#    pypvm.trecv (10,tids[0],1)
    
    results = [None, None]
    i = 0
    for sender in tids:
        print("MASTER: receiving data to tid %s..." % sender)
        pypvm.trecv(10, 2, sender)
        results[i] = pypvm.upk()
        print results[i]
        i += 1

# ============================ test kill () ===================================
    print "MASTER: killing slaves..."
    for tid in tids:
        pypvm.kill (tid)

# ============================ receive exit notification ======================
# NOTE: This is in conjunction with the notify test above.
    for tid in tids:
        pypvm.recv (-1, 2)
        print "MASTER: received an exit notification..."

# ============================ test exit () ===================================
    print "MASTER: exiting pvm..."
    pypvm.exit ()

except:
    print "A pypvm error occured!"
    a =  sys.exc_info()
    print str(a[0])
    print a[1]
    #print sys.exc_info()
